
DEFINE VARIABLE qty AS DECIMAL    NO-UNDO.
DEFINE VARIABLE outfile AS CHARACTER  NO-UNDO initial "R3006.csv".

FOR first code_mstr NO-LOCK WHERE code_fldname="HKR3006A" and
                                  code_value = "outpath":
END.
if avail code_mstr then do:
    assign outfile = code_cmmt + outfile.
end.

output to value(outfile) .
put UNFORMAT "nMonth,site,dept,part,qtycomp" skip.
FOR EACH tr_hist NO-LOCK use-index tr_type WHERE  tr_type = "RCT-WO" and
         tr_effdate >= date(month(TODAY - 100),1,YEAR(TODAY - 100)) AND
         SUBSTRING(tr_nbr,5,1) <> "W",
    each wo_mstr no-lock where wo_nbr = tr_nbr
   break by month(tr_effdate) by wo__chr01 by tr_part:

    if first-of(tr_part) then do:
        assign qty = 0.
    end.
    qty = qty + tr_qty_loc.
    if last-of(tr_part) then do:
        if wo__chr01<> "" then do:
            export delimiter "," string(year(tr_effdate),"9999") + "M" +
                                 string(month(tr_effdate),"99")
                                 substring(wo__chr01,1,3) + "000"
                                 wo__chr01 tr_part qty.
        end.
        else do:
            export delimiter "," string(year(tr_effdate),"9999") + "M" +
                                 string(month(tr_effdate),"99") " "
                                 wo__chr01 tr_part qty.
        end.
    end.
END. /* for each tr_hist */
output close.

for first code_mstr exclusive-lock where code_fldname =  "HKR3006A"
                                     and code_value = "lastrun":
end.
if avail code_mstr then do:
     assign code_cmmt = string(today) + " " + string(time,"HH:MM:SS" )
        code_user1 = "OK".
end.
else do:
    create code_mstr.
    assign code_fldname = "HKR3006A"
           code_value = "lastrun"
           code_cmmt = string(today) + " " + string(time,"HH:MM:SS" )
           code_user1 = "OK".
end.

QUIT.
